/**
 * Created by Amaris on 2016/12/6.
 */


var util = require('../utils/util.js');
var daoUtil = require('../utils/daoUtil');
var StockRecommendClass = require('../class/stockrecommend.js');

module.exports = {

    create: function (params, cb) {
        if (!daoUtil.isArray(params)){
            params = [params];
        }
        for (var i=0;i<params.length;i++){
            var validate = daoUtil.validate(['createUid', 'actionType'], params[i]);
            if(!validate.valid){
                cb(validate.error);
                return;
            }
        }
        daoUtil.proxy(function(models){
            models.stockrecommend.create(params, function (err, data) {
                if (err || !data) {
                    console.error(err);
                    cb(util.getSysError());
                }else{
                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    },
    update: function (params, cb) {
        var validate = daoUtil.validate(['rowId'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        daoUtil.proxy(function(models){
            var stockRecommend = new StockRecommendClass();
            stockRecommend.init(params);
            models.stockrecommend.update({rowId:params.rowId},stockRecommend.getParams()).exec(function (err, data) {
                if (err || !data) {
                    console.error(err);
                    cb(util.getSysError());
                }else{
                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    },
    /**
     *
     * @param params
     * @param cb
     */
    getInfo: function (params, cb) {
        var validate = daoUtil.validate(['rowId'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        daoUtil.proxy(function(models){
            var stockRecommend = new StockRecommendClass();
            stockRecommend.init(params);
            models.stockrecommend.findOne(stockRecommend.getParams()).populate('seller').populate('stockId')
                .populate('createUserId').populate('parentPoolId').populate('childPoolId').exec(function (err, data) {
                if (err || !data) {
                    console.error(err);
                    cb(util.getSysError());
                }else{
                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    },
    //根据CreateUid获取列表
    getListByCreateUid: function (params, cb) {
        var models = util.models;
        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }
        var  stockRecommend = new StockRecommendClass();
        var checkResult = stockRecommend.checkParams(['createUid'], params);
        if (checkResult) {
            cb( util.getErrorRes('400', checkResult),null);
            return;
        } else {
            stockRecommend.init(params);
        }
        models.stockrecommend.find(stockRecommend.getParams()).populate('stockId').populate('createUid').exec(function (err, data){
            if (err || !data) {
                var err = new Error();
                err.status = 500;
                err.message = '系统错误';
                cb(err, null);
                return;
            }

            cb(null, util.getSuccessRes(data));
        });
    },
    //根据ActionId获取列表
    getListByActionId: function (params, cb) {
        var models = util.models;
        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }
        var  stockRecommend = new StockRecommendClass();
        var checkResult = stockRecommend.checkParams(['actionId'], params);
        if (checkResult) {
            cb( util.getErrorRes('400', checkResult),null);
            return;
        } else {
            stockRecommend.init(params);
        }
        models.stockrecommend.find(stockRecommend.getParams()).populate('stockId').populate('createUid').exec(function (err, data){
            if (err || !data) {
                var err = new Error();
                err.status = 500;
                err.message = '系统错误';
                cb(err, null);
                return;
            }

            cb(null, util.getSuccessRes(data));
        });
    },
    //获取个股推荐列表
    getListByActionType: function (params, cb) {
        var models = util.models;
        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }
        var  stockRecommend = new StockRecommendClass();
        var  checkResult = stockRecommend.checkParams(['actionType'], params);
        if (checkResult) {
             cb( util.getErrorRes('400', checkResult),null);
             return;
         } else {
            stockRecommend.init(params);
         }
        models.stockrecommend.find(stockRecommend.getParams(),{sort: 'updatedAt desc'}).populate('seller').populate('stockId').populate('createUserId',{where:{name:params.queryStr}}).exec(function (err, data){
            if (err || !data) {
                var err = new Error();
                err.status = 500;
                err.message = '系统错误';
                cb(err, null);
                return;
            }
            cb(null, util.getSuccessRes(data));
        });
    },
    getListByStockId:function (params, cb) {
        var validate = daoUtil.validate(['stockId'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        daoUtil.proxy(function(models){
            var stockRecommend = new StockRecommendClass();
            stockRecommend.init(params);
            models.stockrecommend.find(stockRecommend.getParams()).exec(function (err, data) {
                if (err) {
                    console.error(err);
                    cb(util.getSysError());
                }else{
                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    },
    deleteByActionId: function (params, cb) {
        var models = util.models;
        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }

        var  stockRecommend = new StockRecommendClass();

        var checkResult = stockRecommend.checkParams(['actionId'], params);
        if (checkResult) {
            cb(null, util.getErrorRes('400', checkResult));
            return;
        } else {
            stockRecommend.init(params);
        }

        var rowId = params.rowId;
        models.stockrecommend.destroy(stockRecommend.getParams()).exec(function (err, data) {
            if (err || !data) {
                cb(err, null);
                return;
            }
            cb(null, util.getSuccessRes(data));
        });
    },
    /**
     * 金股、股票池收益率统计
     * @param params
     * @param cb
     */
    getStockCount:function(params, cb){
        var that = this;
        var validate = daoUtil.validate(['startDate','endDate','type'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        daoUtil.proxy(function(models){
            var sql = "select * from (select IFNULL(SUM(REPLACE(realrate, '%', '')),0) sumRate,b.name from  tb_stockrecommend a left join tb_dic_stock b " +
                " on a.stockid = b.rowId  and a.enable = 0 and a.isrecommend = "+params.type+" and a.updatetime  >='"+params.startDate+"' and a.updatetime <='"+params.endDate+"' and a.actionType = 'stockrecommend'  where b.name is not null group by stockid) c order by c.sumRate asc ";
            models.mconference.query(sql,function (err, data) {
                if (err || !data) {
                    console.error(err);
                    cb(util.getSysError());
                }else{
                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    },
    /**
     * 软删除
     * @param params
     * @param cb
     */
    deleteStockRecommend:function (params, cb) {
        var models = util.models;

        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }

        var  stockRecommend = new StockRecommendClass();

        var checkResult = stockRecommend.checkParams(['rowId'], params);
        if (checkResult) {
            cb(util.getErrorRes('400', checkResult), null);
            return;
        } else {
            params.enable='0';
            stockRecommend.init(params);
        }

        models.stockrecommend.update({rowId: params.rowId}, stockRecommend.getParams()).exec(function (err, data) {
            if (err || !data) {
                console.log(err);
                var err = new Error();
                err.status = 500;
                err.message = '系统错误';
                cb(err, null);
                return;
            }

            cb(null, util.getSuccessRes(data));
        });


    }

};



